package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.vo.MemberVo;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.feign.CouponFeiginService;
import com.atguigu.gulimall.member.vo.LoginVo;
import com.atguigu.gulimall.member.vo.RegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author tr
 * @email tr@gmail.com
 * @date 2023-04-19 13:39:44
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private CouponFeiginService couponFeiginService;
    @Autowired
    private MemberService memberService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        MemberVo member = memberService.login(loginVo);
        if(member==null){
            //登录失败
            return R.error(BizCodeEnum.LOGIN_ERROR_EXCEPTION.getCode(), BizCodeEnum.LOGIN_ERROR_EXCEPTION.getMsg()).put("msg", BizCodeEnum.LOGIN_ERROR_EXCEPTION.getMsg());
        }
        else {
            //登录成功
            return R.ok().setData(member);
        }
    }

    @PostMapping("/regist")
    public R regist(@RequestBody RegistVo registVo){
        try {
            memberService.regist(registVo);
        }catch (UsernameExistException e){
            //注册失败，用户名或者手机号已经重复
            return R.error(BizCodeEnum.USER_EXIST__EXCEPTION.getCode(),BizCodeEnum.USER_EXIST__EXCEPTION.getMsg());
        }catch (PhoneExistException e){
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @GetMapping("memberconpon")
    public R test(){
        R test = couponFeiginService.membercoupons();
        Object coupons = test.get("coupons");
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername("测试会员");
        return R.ok().put("member",memberEntity).put("coupon",coupons);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
